package peppa.george.movie.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import peppa.george.base.Util.JsonResult;
import peppa.george.base.Util.PageUtil;
import peppa.george.movie.domain.Movie;
import peppa.george.movie.query.MovieQuery;
import peppa.george.movie.service.IMovieService;

import java.util.List;

@RestController
@RequestMapping("/movie")
public class MovieController {

    @Autowired
    private IMovieService movieService;

    // 查询全部
    @GetMapping
    public List<Movie> findAll(){
        return movieService.findAll();
    }
    // 查询单条
    @GetMapping("/{id}")
    public Movie findById(@PathVariable("id")Long id){
        return movieService.findById(id);
    }
    // 删除
    @DeleteMapping("/{id}")
    public JsonResult delete(@PathVariable("id")Long id){
        movieService.delete(id);
        return JsonResult.me().setMsg("请求成功");
    }
    // 修改
    @PutMapping
    public JsonResult saveOrUpdate(@RequestBody Movie movie){
        if (movie.getId() == null){
            movieService.save(movie);
        } else {
            movieService.update(movie);
        }
        return JsonResult.me().setMsg("请求成功");
    }
    @PostMapping
    public PageUtil<Movie> queryByPage(@RequestBody MovieQuery movieQuery){
        return movieService.queryByPage(movieQuery);
    }
}
